package com.joizhang.seckill.mapper;

import com.joizhang.seckill.model.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

@Repository
@Mapper
public interface UserMapper {

    @Select("select * from sk_user where id=#{id}")
    User getUserById(@Param("id") long id);

    @Select("select * from sk_user where phone_number=#{credential} or username=#{credential}")
    User getUserByCredential(@Param("credential") String credential);

    @Select("select * from sk_user where phone_number=#{phoneNumber}")
    User getUserByPhoneNumber(@Param("phoneNumber") String phoneNumber);

    @Insert("insert into sk_user (username, password, salt, phone_number, avatar, register_date, last_login_date, login_count) " +
            "values (#{username}, #{password}, #{salt}, #{phoneNumber}, #{avatar}, #{registerDate}, #{lastLoginDate}, #{loginCount})")
    void insertUser(User user);

    @Update("update sk_user set password=#{password} where id=#{id}")
    void update(User toBeUpdate);

    @Select("select * from sk_user where username=#{username}")
    User getUserByUsername(@Param("username") String username);

}
